package LeetCode._05_堆;

import java.util.PriorityQueue;

public class _703_数据流中的第K大元素 {
    class KthLargest {
        private int k;
        private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        public KthLargest(int k, int[] nums) {
            for(int num : nums){
                minHeap.offer(num);
                if(minHeap.size() > k){
                    minHeap.poll(); //堆中只保留前K个元素
                }
            }
            this.k = k; //初始化
        }

        public int add(int val) {
            minHeap.offer(val);
            if(minHeap.size() > k){
                minHeap.poll();
            }
            return minHeap.peek();
        }
    }
}
